# 用flask框架写一个最小化web 服务器端程序
from flask import Flask, request, render_template, jsonify  # 导入Flask类
# import time,detectdish,detectljfl
import time, mydish, detectljfl, myocr,  generalapp, detectMedicine,detectFish,detectChina

app = Flask(__name__)  # 创建核心对象app


# 设置路由
@app.route('/')
def home():
    # return 'welcome to flask web server'
    return render_template('index.html')


@app.route('/nCoV/api/area')
def foo():
    print(request)
    print(request.method)
    print(request.headers)
    print(request.args)
    print(request.args.get('latest'))
    print(request.args.get('province'))
    print(request.args.get('name'))

    return 'ok'


@app.route('/upload', methods=['GET', 'POST'])
def upload():
    if request.method == 'POST':
        f = request.files['file']  # f对象包含文件相关的信息(文件流,文件名)
        uploadfile = 'static/images/' + f.filename
        f.save(uploadfile)  # 保存上传文件到服务器
        currentfile = 'static/images/current.jpg'  # 保存当前最新上传的图片文件
        # 复制上传的图片文件到当前的图片文件中
        with open(uploadfile, 'rb') as fs, open(currentfile, 'wb') as fd:
            fd.write(fs.read())

        print(f.filename)
    return render_template('dish.html', val=time.time())


# @app.route('/vis')
# def vis():
#     # 向百度后台发起图像识别
#     # result = detectdish.detect_dish() # 返回的是字典类型
#     result = mydish.mydish()
#     return jsonify(result)  # 给客户端生成application/json响应


@app.route('/vis', methods=['POST', 'GET'])
def do_detect():
    if request.method == 'POST':
        param = request.form
        print(param)
        img = param.get('image')
        r = mydish.mydish(img)

        print('data:image/jpg;base64,', img)
        print('##')
        print(r)

        return jsonify(r)
    else:
        return 'this is detect get request'


@app.route('/ocr', methods=['POST', 'GET'])
def do_ocr():
    if request.method == 'POST':
        param = request.form
        print(param)
        img = param.get('image')
        r = myocr.myocr(img)

        print('data:image/jpg;base64,', img)
        print('##')
        print(r)

        return jsonify(r)
    else:
        return 'this is detect get request'


@app.route('/rub', methods=['GET', 'POST'])
def rubvis():
    if request.method == 'POST':
        param = request.form
        print(param)
        img = param.get('image')
        r = detectljfl.detect_rubbish(img)
        print('data:image/jpg;base64,', img)
        print('##')
        print(r)

        return jsonify(r)
    else:
        return 'this is detect get request'


@app.route('/xxx', methods=['POST'])
def xxxA():
    if request.method == 'POST':
        f = request.files['file']  # f对象包含文件相关的信息(文件流,文件名)
        f.save('static/images/' + f.filename)  # 保存上传文件到服务器
        print(f.filename)
    return '哥们你上传,我不会弄,我知道文件名:' + f.filename + ' 我会保存!'


@app.route('/uploadljfl', methods=['GET', 'POST'])
def uploadljfl():
    if request.method == 'POST':
        f = request.files['file']  # f对象包含文件相关的信息(文件流,文件名)
        uploadfile = 'static/images/' + f.filename
        f.save(uploadfile)  # 保存上传文件到服务器
        currentfile = 'static/images/current.jpg'  # 保存当前最新上传的图片文件
        # 复制上传的图片文件到当前的图片文件中
        with open(uploadfile, 'rb') as fs, open(currentfile, 'wb') as fd:
            fd.write(fs.read())

        print(f.filename)
    return render_template('rubbish.html', val=time.time())


@app.route('/detect', methods=['POST', 'GET'])
def do_general():
    if request.method == 'POST':
        param = request.form
        print(param)
        img = param.get('image')
        r = generalapp.general_detect(img)

        print('data:image/jpg;base64,', img)
        print('##')
        print(r)

        return jsonify(r)
    else:
        return 'this is detect get request'


@app.route('/fish', methods=['POST', 'GET'])
def test_fish():
    if request.method == 'POST':
        param = request.form
        print(param)
        img = param.get('image')
        r = detectFish.fish_detect(img)
        print('data:image/jpg;base64,', img)
        print('##')
        print(r)

        return jsonify(r)
    else:
        return 'this is detect get request'


@app.route('/zhongyao', methods=['POST', 'GET'])
def do_zhongyao():
    if request.method == 'POST':
        param = request.form
        print(param)
        img = param.get('image')
        r = detectMedicine.query_zy(img)
        return jsonify(r)
    else:
        return 'this is detect get request'


@app.route('/taoci', methods=['POST', 'GET'])
def do_taoci():
    if request.method == 'POST':
        param = request.form
        print(param)
        img = param.get('image')
        r = detectChina.detect_taoci(img)
        return jsonify(r)
    else:
        return 'this is detect get request'

# 启动web 服务器端程序
if __name__ == '__main__':
    app.run(host='192.168.43.164', debug=True)
    # ip=0.0.0.0 说明服务器端程序可在外部访问
